<%--
  Created by IntelliJ IDEA.
  User: 小龙
  Date: 2019/8/18
  Time: 20:32
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


<html>
<head>
    <title>管理员信息表</title>
    <link href="../layui/css/layui.css" type="text/css" rel="stylesheet" />
    <script src="../js/jquery-1.12.4.js"></script>
    <script src="../layui/layui.all.js" charset="utf-8"></script>
    <style type="text/css">
        .layui-table-cell{
            height: auto;
            line-height: 28px;
        }
    </style>
    <script src="../layui/layui.js" type="text/javascript"></script>
    <script type="text/javascript" src="../js/jquery-1.12.4.js"></script>
    <script type="text/javascript">
        layui.use('table', function(){
            var table = layui.table;
            table.render({
                elem: "#myTable",
               //提交数据的地址
                url: "/admin/selectAll",
                //开启分页,
                page: true,
                toolbar:"#myToolbar",
                cols: [[
                    {type:"checkbox"},
                    {title:"编号",field:"adminId"},
                    {title:"管理员账号",field:"userName"},
                    {title:"管理员密码",field:"password"},
                    {title:"操作",templet:"#CaoZuo"}
                ]]
            });
        });

         layui.use(["form","layer","table"],function () {
            var form = layui.form;
            var layer = layui.layer;
            var table = layui.table;
            // 当点击提交按钮的时候，会进入到这个函数
            // 当所有的验证都通过时，才会进入到这个函数
            form.on("submit(update)", function (data) {
                $.ajax({
                    url: "/admin/update",
                    data: data.field,
                    type: "post",
                    success: function (data) {
                        //console.log(data)
                        //1.关闭掉添加弹出层
                        layer.closeAll('page');
                        //2.提示添加成功
                        if (data.isUpdate) {
                            layer.alert("修改成功", {time: 3000});
                        } else {
                            layer.alert("修改失败", {time: 3000});
                        }
                        //3.刷新table
                        table.reload("myTable");
                    }
                })
                return false;//阻止跳转；
            });
             form.on("submit(insert)", function (data) {
                 $.ajax({
                     url: "/admin/insert",
                     data: data.field,
                     type: "post",
                     success: function (data) {
                         //console.log(data)
                         //1.关闭掉添加弹出层
                         layer.closeAll('page');
                         //2.提示添加成功
                         if (data.isInsert) {
                             layer.alert("添加成功", {time: 3000});
                         } else {
                             layer.alert("修改失败", {time: 3000});
                         }
                         //3.刷新table
                         table.reload("myTable");
                     }
                 })
                 return false;//阻止跳转；
             });

        });
            //修改操作
            function toOpenUpdateLayer(id) {
                //1.获取当前行数据===》发送ajax请求，获取当前行数据
                var form=layui.form;
                $.ajax({
                    url: "/admin/selectById",
                    data: "id=" + id,
                    success: function (data) {
                        $("#adminid").val(data.adminId);
                        $("#adminusername").val(data.userName);
                        $("#adminpassword").val(data.password);

                    }
                });
                //2.把数据填充到修改弹出层中==>弹出层显示
                $("#updateSubmitBtn").show();
                layui.use('layer', function () {
                    layer.open({
                        title: "修改管理员信息",
                        content: $("#updateFrom"),
                        type: 1,
                    })
                });
            }
            //添加弹出层
            function toOpenAddLayer() {
                layui.use('layer', function () {
                    $("#insertFrom")[0].reset();
                    layer.open({
                        title: "添加管理员",
                        content: $("#insertFrom"),
                        type: 1,
                    })
                });

            }





            //删除一条数据
        function doDelete(id) {
            //确认；如果点击确认删除；否则不删除
            layui.use(['layer', 'table'], function () {
                var table = layui.table;
                layer.confirm('确定要杀掉这个沙雕该管理员吗？', {icon: 3, title: '确认删除'}, function (index) {
                    $.ajax({
                        url: "/admin/delete",
                        data: "id=" + id,
                        success: function (data) {
                            layer.alert(data.isDelete, {time: 2000});
                            table.reload("myTable");
                            layer.close(index);
                        }
                    })
                });
            });



        }



        // 下载全部数据
        // function doDownload() {
        //     //确认；如果点击确认删除；否则不删除
        //     layui.use(['layer','table'], function(){
        //         var table = layui.table;
        //         layer.confirm('确定要导出全部数据嘛？', {icon: 3, title:'确认导出'}, function(index){
        //             $.ajax({
        //                 url:"/guru/download",
        //                 success:function(data){
        //                     layer.alert(data.isxiazai,{time:2000});
        //                     table.reload("myTable");
        //                     layer.close(index);
        //                 }
        //             })
        //         });
        //     });
        // }









    </script>

    <script id="CaoZuo" type="text/html">
        <button type="button" class="layui-btn layui-btn-radius layui-btn-normal" onclick="toOpenUpdateLayer('{{d.adminId}}')">修改</button>
        <button type="button" class="layui-btn layui-btn-danger layui-btn-radius" onclick="doDelete('{{d.adminId}}')">删除</button>
    </script>

</head>
<body>
<%--<table id="myTable"></table>--%>
<table id="myTable" lay-data="{id: 'myTable'}"></table>
<div id="myToolbar" style="display: none;">
    <button type="button" class="layui-btn layui-btn-normal layui-btn-radius" onclick="toOpenAddLayer()">
        <i class="layui-icon">&#xe608;</i> 添加管理员
    </button>
    <%--<button type="button" class="layui-btn layui-btn-normal layui-btn-radius" onclick="doDownload()">--%>
        <%--<i class="layui-icon">导出上师数据</i></button>--%>
    </div>
</div>



<%--修改学生信息--%>
<form id="updateFrom" style="display: none" class="layui-form">
    <div class="layui-form-item">
        <label class="layui-form-label">管理员编号</label>
        <div class="layui-input-block">
            <input name="adminId" id="adminid" class="layui-input" value="0" readonly/>
        </div>
    </div>


    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">管理员账号</label>
        <div class="layui-input-block">
            <input name="userName" id="adminusername" class="layui-input" placeholder="请输入管理账号" />
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">管理员密码</label>
        <div class="layui-input-block">
            <input name="password" id="adminpassword" class="layui-input" placeholder="请输入管理员密码" />
        </div>
    </div>
    <div class="layui-form-item">
        <button lay-submit type="button" lay-filter="update" class="layui-btn" id="updateSubmitBtn">修改管理员信息</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
</form>




<%--添加管理员信息--%>
<form id="insertFrom" style="display: none" class="layui-form">
    <div class="layui-form-item">
        <label class="layui-form-label">管理员账号</label>
        <div class="layui-input-block">
            <input name="userName" id="username"  class="layui-input" placeholder="请输入管理员账号" />
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">管理员密码</label>
        <div class="layui-input-block">
            <input name="password"  id="password" class="layui-input" placeholder="请输入管理员密码" />
        </div>
    </div>


</div>
    <div class="layui-form-item">
        <button lay-submit type="button" lay-filter="insert" class="layui-btn" id="insertSubmitBtn">添加管理员信息</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
</form>



</body>
</html>
